package com.ht.shop.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.ht.shop.dao.GoodsTypeDao;
import com.ht.shop.dao.impl.GoodsTypeDaoImpl;
import com.ht.shop.pojo.TbGoodsType;
import com.ht.shop.service.GoodsTypeService;


import java.sql.SQLException;
import java.util.List;

public class GoodsTypeServiceImpl implements GoodsTypeService {
    private GoodsTypeDao goodsTypeDao = new GoodsTypeDaoImpl();
    @Override
    public List<TbGoodsType> findAllSecondLevelType() throws SQLException {
        return goodsTypeDao.findAllSecondLeveType();
    }

    @Override
    public void addType(String name, String parent) throws Exception {

        if (StringUtils.isEmpty(name)||StringUtils.isEmpty(parent)) {
            throw new RuntimeException();
        }
        goodsTypeDao.addType(name,parent);
    }

    @Override
    public int deleteType(String id) {
        List<TbGoodsType> typeByPIdAndStaus = findTypeByPIdAndStaus(id, 1);
        if (typeByPIdAndStaus == null||typeByPIdAndStaus.size()==0) {
            return goodsTypeDao.deleteType(id);
        }
        return -1;
    }

    @Override
    public List<TbGoodsType> findTypeByPIdAndStaus(String pid, int status) {
        return goodsTypeDao.findIdByPIdAndStaus(pid, status);
    }
}
